from Agent.Astar.Astar_Home import *
import math

# 凶狠贪婪算法
# 搜索五步以内最大值
def CruelGreed(env,nowstate,player,deepth):

    points = env.getAllLegalMoveAroud(nowstate,player,deepth)
    Score = {}
    for point in points:
        if point in env.reward_states.keys():
            aStar = AStar(startpoint=nowstate, endpoint=point, env=env, player=player)
            action, length = aStar.action()
            if action == None:
                continue

            Score[point] = env.reward_states[point]
            moves = env.getAllLegalMove(point,player)
            moves.remove("S")
            #算他的上下左右,加到这个点上
            for move in moves:
                nowkey = env.getActionNum(point,move)
                if nowkey in env.reward_states.keys():
                    Score[point] = Score[point] + env.reward_states[nowkey]

            #保证稳定性
            Score[point] = Score[point] * math.pow(length, -0.4)


    # 返回自己和地方的点
    if player == 1:
        nowstate = env.play1state
        opponent = env.play2state
    if player == 2:
        nowstate = env.play2state
        opponent = env.play1state

    #对字典进行排序
    result = sorted(Score.items(),key=lambda item:item[1],reverse=True)


    # 判断豆最多的点
    for result_point in result:

         aStar = AStar(startpoint=nowstate, endpoint=result_point[0], env=env, player=player)
         action, length = aStar.action()
         if action is not None:
             return action



